package com.dap.dao.interceptor;

public class SelectForUpdateHelper {

    private static String updateSql = " FOR UPDATE ";

    /**
     * 保存分页查询时是否需要总记录数
     */
    private static ThreadLocal<Boolean> selectForUpdateholder = new ThreadLocal<Boolean>() {
        protected Boolean initialValue() {
            return Boolean.FALSE;
        }
    };


    public static boolean isSelectForUpdate() {
        return selectForUpdateholder.get();
    }


    public static void setSelectForUpdate() {
        selectForUpdateholder.set(true);
    }


    public static void cancelSelectForUpdate() {
        selectForUpdateholder.set(false);
    }


    public static String getUpdateSql() {
        return updateSql;
    }


    /** waitTime 需要配置为 "wait 30" */
    public void setWaitTime(String waitTime) {
        String updateSql = waitTime.toUpperCase();
        if (updateSql.length() > 0 && updateSql.indexOf("WAIT") == -1) {
            updateSql = "WAIT " + waitTime;
        }
        SelectForUpdateHelper.updateSql = " FOR UPDATE " + updateSql;
    }
}